<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/1 0001
 * Time: 07:05
 */

namespace Admin\Controller;


use Think\Controller;

class IndexController extends Controller{

    public function _initialize(){
        if(!session("user_id")){
            $this->redirect("User/index");
        }
    }
    public function index(){

        if(IS_POST){
            $lotteryModel = M("Lottery");
            if($lotteryModel->create()){
                if(I("post.id")){
                    if($lotteryModel->save()){
                        $this->success("更新成功");
                    }
                }else{
                    if($lotteryModel->add()){
                        $this->success("添加成功");
                    }
                }

            }

        }else{
            $userList = M("User")->select();
            $vUserList = M("VirtualUser")->select();
            $lotteryList = M("Lottery")->select();
            $currentNum = 0;
            foreach($lotteryList as $lottery){
                $currentNum += $lottery['num'];
            }
            $this->assign('lastNum',count($userList)+count($vUserList)-$currentNum);
            $this->assign("userList",$userList);
            $this->assign("vUserList",$vUserList);
            $this->assign("lotteryList",$lotteryList);
            $this->display();
        }

    }


    public function logout(){
        session('user_id',null);
        $this->redirect("User/index");
    }

    public function userList(){
        if(IS_POST){
            $data['phone'] = I("post.phone");
            $data['name'] = I("post.name");
            if(!I("post.editPic")){
                //增加或编辑状态下，上传了图片
                $config = array(
                    'maxSize' => 3145728,
                    'rootPath' => './Uploads/',
                    'savePath' => 'Virtual/',
                    'saveName' => array('uniqid', ''),
                    'exts' => array('jpg', 'gif', 'png', 'jpeg'),
                    'autoSub' => true,
                    'subName' => array('date', 'Ymd'),
                );
                $upload = new \Think\Upload($config);// 实例化上传类
                $info = $upload->upload();
                if ($info) {
                    $data['pic'] = $info['pic']["savepath"] . $info['pic']["savename"];
                }
            }

            if(!I('post.id')){
                //增加
                if(M("VirtualUser")->where(array("name"=>I("post.name")))->find() || M("User")->where(array("name"=>I("post.name")))->find()) {
                    $this->error("该用户名已存在");
                }else if(M("VirtualUser")->where(array("phone"=>I("post.phone")))->find() || M("User")->where(array("phone"=>I("post.phone")))->find()){
                    $this->error("该手机号码已存在");
                }
                $data['uniqId'] = uniqid();
                if (M("VirtualUser")->add($data)) {
                    $this->success('添加成功');
                }
            }else{
                //编辑
                $data["id"] = I("post.id");
                if(M("VirtualUser")->save($data)){
                    $this->success('编辑成功');
                }
            }
        }else{
            $userModel = M("user");
            $count = $userModel->count();
            $page = new \Think\Page($count,10);
            $page->setConfig("prev","上一页");
            $page->setConfig("next","下一页");
            $show = $page->show();
            $userList = $userModel->limit($page->firstRow.','.$page->listRows)->select();
            $this->assign("userList",$userList);
            $this->assign("page",$show);

            $vUserModel = M("VirtualUser");
            $count = $vUserModel->count();
            $vPage = new \Think\Page($count,10);
            $vPage->setConfig("prev","上一页");
            $vPage->setConfig("next","下一页");
            $vShow = $vPage->show();
            $vUserList = $vUserModel->limit($page->firstRow.','.$page->listRows)->select();
            $this->assign("vUserList",$vUserList);
            $this->assign("vPage",$vShow);
            $this->display("userlist");
        }



    }

    public function getLotteryById($id){
        $this->ajaxReturn(M("Lottery")->where(array("id"=>$id))->find());
    }

    public function getVirtualUserById($id){
        $this->ajaxReturn(M("virtualUser")->where(array("id"=>$id))->find());
    }

    public function checkLotteryUser($uniqid){
        $lotteryList = M("lottery")->select();
        $lotteryUserList = array();
        foreach($lotteryList as $lottery){
            if($lottery["lotteryuser"]){
                $lotteryUserList = array_merge($lotteryUserList,explode(",",$lottery["lotteryuser"]));
            }
        }
        $test = in_array($uniqid,$lotteryUserList);
        $this->ajaxReturn(in_array($uniqid,$lotteryUserList));
    }

    public function resetLottery(){
        session("lotteryId",null);
        session("idIndex",null);
        session("lotIdList",null);
        session("lotteryUserList",null);
        $this->redirect('Index/index');
    }

}